package org.docmanagement.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.taglib.TagUtils;
import org.docmanagement.commons.Constants;
import org.docmanagement.forms.TodoForm;

/**
 * @author eveno
 *
 */
public class TodoStatusTag extends TagSupport implements Constants
{
	private static final long serialVersionUID = 1L;

    private String beanName = null;
    private String property = null;
    private String scope = "request";

	/**
	 * 
	 */
	public TodoStatusTag()
	{
		super();
	}

    public String getName()
    {
        return beanName;
    }

    public void setName(String n)
    {
        this.beanName = n;
    }

    public String getProperty()
    {
        return property;
    }

    public void setProperty(String pProperty)
    {
        this.property = pProperty;
    }
    /**
     * @return Returns the scope.
     */
    public String getScope()
    {
        return scope;
    }
    /**
     * @param scope The scope to set.
     */
    public void setScope(String scope)
    {
        this.scope = scope;
    }

    public int doStartTag() throws JspException
    {
        return SKIP_BODY;
    }

    public int doEndTag() throws JspException
    {
        TagUtils tagUtils = TagUtils.getInstance();
        int status = 0;
        if (property == null)
        {
	        Object o = tagUtils.lookup(pageContext, beanName, scope);
	        if (!(o instanceof TodoForm) )
	            throw new JspException(
	                    tagUtils.message(
	                            pageContext,
	                            Globals.MESSAGES_KEY,
	                            Globals.LOCALE_KEY,
	                            "error.bean.wrongtype",
	                            new Object[] {
	                                    beanName,
	                                    TodoForm.class.getName()
	                            }
	                    )
	            );
	        status = ((TodoForm)o).getStatus();
		}
		else
		{
	        Object o = tagUtils.lookup(pageContext, beanName, property, scope);
	        status = ((Integer) o).intValue();
		}
	
//        String label = tagUtils.message(
//                pageContext,
//                Globals.MESSAGES_KEY,
//                Globals.LOCALE_KEY,
//                getStatusLabel(status)
//        );
        tagUtils.write(pageContext, getStatusLabel(status)/*label*/);
        return EVAL_PAGE;
    }

    /**
     * @param type
     * @return
     */
    private String getStatusLabel(int status)
    {
        switch (status)
        {
            case TODO_STARTED:
                return TODO_STARTED_STRING;
            case TODO_FINISHED:
                return TODO_FINISHED_STRING;
            default:
                return "";
        }
    }

    public void release()
    {
        super.release();
        beanName = null;
        property = null;
        scope = "request";
    }
}
